//
//  NEWSVideoTableViewController.m
//  HeXun
//
//  Created by 8003 on 15/11/19.
//  Copyright © 2015年 LGC. All rights reserved.
//

#import "NEWSVideoTableViewController.h"
#import "NewsVideoTableViewCell.h"
#import "GetXMLObject.h"
#import "NEWSModel.h"
#import <AVFoundation/AVFoundation.h>
#import "NEWSModel.h"
#import "UIImageView+WebCache.h"
#import "VideoViewController.h"
#import "MBProgressHUD.h"
@interface NEWSVideoTableViewController ()
{
    NSArray* _videoNewsArr;
    NSArray* _videoURLArr;
}
@end

@implementation NEWSVideoTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSBlockOperation* cOperation = [NSBlockOperation blockOperationWithBlock:^{
        
        GetXMLObject* xmlObject = [GetXMLObject new];
        _videoNewsArr = [xmlObject getVideoNEWSArrayFromBlockID:129580746];
        
        NSMutableArray* tempArr = [NSMutableArray new];
        for (NEWSModel* newsModel in _videoNewsArr)
        {
            NSURL* url = [xmlObject getVideoNewsURLFromVideoNEWSID:newsModel.ID];
            [tempArr addObject:url];
        }
        _videoURLArr = [NSArray arrayWithArray:tempArr];
        
        
        NSBlockOperation* dOperation = [NSBlockOperation blockOperationWithBlock:^{
            [self.tableView reloadData];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
        //涉及更新UI的操作，就要创建包含这些操作的Operation对象，并把它放入主线程队列
        NSOperationQueue* mainQueue = [NSOperationQueue mainQueue];
        //当Operation放入主线程队列中，其操作就是子主线程中执行
        [mainQueue addOperation:dOperation];
    }];
    
    //创建Operation队列,operation只有在队列里才能跑
    NSOperationQueue* takeMoneyQueue = [NSOperationQueue new];
    
    [takeMoneyQueue addOperation:cOperation];
    
    
   }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _videoNewsArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GetXMLObject* getXMLObject = [GetXMLObject new];
    NewsVideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"videocell" forIndexPath:indexPath];
    NEWSModel* newsModel = _videoNewsArr[indexPath.row];
    cell.titleLabel.text = newsModel.title;
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:[getXMLObject getStrArrFrom:newsModel.matchpic using:@"|"].firstObject]];

    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    VideoViewController* videoViewController = segue.destinationViewController;
    videoViewController.url = _videoURLArr[[self.tableView indexPathForSelectedRow].row];
}


@end
